package com.zjuh.goodboy.util;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupUtil {
	
	public static String getLinkText(Element element) {
		String text = element.text();
		if (!StringUtil.isEmpty(text)) {
			return text;
		}
		text = element.attr("title");
		if (!StringUtil.isEmpty(text)) {
			return text;
		}
		return JsoupUtil.getText(element);
	}
	
	private static String getText(Element element) {
		String text = element.text();
		if (!StringUtil.isEmpty(text)) {
			return text;
		}
		Elements childElements = element.children();
		for (Element el : childElements) {
			if (el.nodeName().toLowerCase().equals("img")) {
				text = el.attr("alt");
				if (!StringUtil.isEmpty(text)) {
					return text;
				}
 			} else {
 				String s = el.text();
 				if (!StringUtil.isEmpty(s)) {
 					return s;
 				}
 				s = getText(el);
 				if (StringUtil.isEmpty(s)) {
 					return s;
 				}
 			}
		}
		return null;		
	}
	
}
